const HOME_PAGE_PATH = 'pages/index/index'
const SETTING_PAGE_PATH = 'pages/index/setting'
const platformInfo = process.env.uniTestPlatformInfo.toLocaleLowerCase()
const isIos = platformInfo.startsWith('ios')
const isWeb = platformInfo.startsWith('web')
const isMP = platformInfo.startsWith('mp')
const isHarmony = platformInfo.startsWith('harmony')
const navigateToTimeout = 9000

describe('globalProperties', () => {
  it('navigateTo in onLaunch', async () => {
    // 等待页面跳转完成
    await new Promise(resolve => {
      setTimeout(() => {
        resolve()
      }, isWeb ? 2000 : 1000)
    })
    let errMsg = ''
    let page = await program.currentPage()

    if (page.path !== SETTING_PAGE_PATH) {
      const start = Date.now()
      page.waitFor(async () => {
        page = await program.currentPage()
        if (Date.now() - start > navigateToTimeout) {
          errMsg = '超时时间内未跳转完成'
          return true
        }
        return page.path  == SETTING_PAGE_PATH || page.path == `/${SETTING_PAGE_PATH}`
      })
      expect(errMsg).toBe('')
    }

    await page.callMethod('getState')
    const data = await page.data()
    if (isIos || isHarmony) {
      // ios 端 onLaunch 中 navigateTo 不会触发 lock 机制
      expect(data.lifeCycleNum).toBe(16)
      expect(data.pages.length).toBe(4)
      expect(data.pages[0]).toBe(HOME_PAGE_PATH)
      expect(data.pages[1]).toBe(SETTING_PAGE_PATH)
      expect(data.pages[2]).toBe(SETTING_PAGE_PATH)
      expect(data.pages[3]).toBe(SETTING_PAGE_PATH)
    } else if(isMP){
      // mp 端 onLaunch 中 navigateTo 不会触发 lock 机制
      // 但是页面只加载一次
      expect(data.lifeCycleNum).toBe(14)
      expect(data.pages.length).toBe(2)
      expect(data.pages[0] == HOME_PAGE_PATH || data.pages[0] == '/').toBe(true)
      expect(data.pages[1] == SETTING_PAGE_PATH || data.pages[1] == `/${SETTING_PAGE_PATH}`).toBe(true)
    } else {
      expect(data.lifeCycleNum).toBe(10)
      expect(data.pages.length).toBe(2)
      expect(data.pages[0] == HOME_PAGE_PATH || data.pages[0] == '/').toBe(true)
      expect(data.pages[1] == SETTING_PAGE_PATH || data.pages[1] == `/${SETTING_PAGE_PATH}`).toBe(true)
    }
  })
})